#!/bin/bash
#Author:        rengyi yan
#Date Time:     2015-08-03 10:40:41
#Descriptions:  auto install redis to the /usr/local/redis by source code
#               setting the redis server run as a daemon service
#               the configfile is /etc/redis/redis.conf
#               the logfile is /var/log/redis/redis.log
#               and the db storaged dir is /usr/local/redis/dbs

 RETVAL=0
 REDIS_VERSION=0
 REDIS_DIR=/usr/local/redis
 REDIS_DB_DIR=$REDIS_DIR/dbs
 PIDFILE=/var/run/redis.pid
 LOGFILE=/var/log/redis/redis.log
 CONFIGFILE=/etc/redis/redis.conf


#Step 1: download the redis source code
 BASE="http://download.redis.io/releases/redis-"
 read -p "please input the redis version like x.y.z:   " REDIS_VERSION
 URL=${BASE}${REDIS_VERSION}.tar.gz
 echo "###########downloading redis source code##########"
 wget $URL
 tar -xzvf redis-*.tar.gz -C /usr/local
 rm redis-*.tar.gz

#Step 2: rename redis-x.y.z to /usr/local/redis
 mv /usr/local/redis-${REDIS_VERSION} $REDIS_DIR
 cd  $REDIS_DIR
 #compile to generate the executable files
  make

#Step 3: create symbolic link for executable files
 ln -s $REDIS_DIR/src/redis-server /usr/sbin/redis-server
 ln -s $REDIS_DIR/src/redis-cli /usr/sbin/redis-cli
 ln -s $REDIS_DIR/src/redis-benchmark /usr/sbin/redis-benchmark
 ln -s $REDIS_DIR/src/redis-check-aof /usr/sbin/redis-check-aof
 ln -s $REDIS_DIR/src/redis-check-dump /usr/sbin/redis-check-dump
 ln -s $REDIS_DIR/src/redis-sentinel /usr/sbin/redis-sentinel

#Step 4: modify the default config file
 mkdir /etc/redis
 mkdir /var/log/redis
 mkdir $REDIS_DB_DIR
 touch /var/log/redis/redis.log
 cp $REDIS_DIR/redis.conf /etc/redis
 sed -i 's/daemonize no/daemonize yes/g' $CONFIGFILE
 sed -i 's/logfile ""/logfile \/var\/log\/redi\s\/redi\s.log/g' $CONFIGFILE
 sed -i 's/dir .\//dir  \/u\sr\/local\/redi\s\/db\s/g' $CONFIGFILE

 echo "#####################redis install complete!########################"
